package com.bee.computer.impl;

import java.io.*;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * @version 1.0.0
 * @Author yong.Mr
 * @data 2019-8-9
 */
public class NIOTestService {

    public static void main(String[] args) {
        try {
            OutputStream outputStream = new FileOutputStream("basis.txt");
            FileChannel channel = ((FileOutputStream) outputStream).getChannel();
            ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
            byteBuffer.put("hello nio".getBytes());
            byteBuffer.flip();
            channel.write(byteBuffer);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * nio相文件write数据
     */
    public void nioWriteTest() {
        OutputStream outputStream = null;
        try {
            outputStream = new FileOutputStream("basis.txt");
            FileChannel channel = ((FileOutputStream) outputStream).getChannel();
            ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
            byteBuffer.put("hello nio".getBytes());
            byteBuffer.flip();
            channel.write(byteBuffer);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if(outputStream != null) {
                try {
                    outputStream.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * nio相文件read数据
     */
    public void nioReadTest() {
        InputStream inputStream = null;
        try {
            inputStream = new FileInputStream("basis.txt");
            FileChannel channel = ((FileInputStream) inputStream).getChannel();
            ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
            channel.read(byteBuffer);
            System.out.println(new String(byteBuffer.array()));
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if(inputStream != null) {
                try {
                    inputStream.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * nio文件的copy, transferTo, transferTo
     */
    public void nioCopyTest () {
        InputStream inputStream = null;
        OutputStream outputStream = null;
        try {
            inputStream = new FileInputStream("basis.txt");
            outputStream = new FileOutputStream("dest.txt");
            FileChannel sourceChannel = ((FileInputStream) inputStream).getChannel();
            FileChannel destChannel = ((FileOutputStream) outputStream).getChannel();
            // transferTo 将本身通道的数据copy到目标通道--->形参中的通道
            sourceChannel.transferTo(0,sourceChannel.size(),destChannel);
            // transferTo 将目标通道的数据--->形参中的通道 copy到本身通道
            destChannel.transferFrom(sourceChannel,0,sourceChannel.size());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if(inputStream != null) {
                try {
                    inputStream.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            if(outputStream != null) {
                try {
                    outputStream.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

    }
}
